package br.com.jr.arq.dominio;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

import br.com.jr.arq.dao.Persistente;

@Entity
@Table(name = "users", schema="comum")
public class Usuario implements UserDetails, Persistente {

	// Original props
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id_user", unique = true, nullable = false)
	private int id;
	
	@Column(name = "username")
	private String username;
	
	@Column(name = "password")
    private String password;
	
	@Column(name = "enabled")
    private Boolean enabled;

	private transient Collection<GrantedAuthority> authorities;
	 
	@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinTable(name = "user_authorities", schema="comum", joinColumns = { @JoinColumn(name = "id_user")},
    inverseJoinColumns={@JoinColumn(name="id_authority")})
	private Set<Authority> authoritySet = new HashSet<Authority>();
    
	@Transient
	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Transient
	public boolean isAccountNonExpired() {
		return true;
	}

	@Transient
	public boolean isAccountNonLocked() {
		return true;
	}
	
	@Transient
	public boolean isCredentialsNonExpired() {
		return true;
	}
	
	@Transient
	public boolean isEnabled() {
		return getEnabled();
	}
	
	@Transient
	public void setUserAuthorities(List<String> authorities) {
		List<GrantedAuthority> listOfAuthorities = new ArrayList<GrantedAuthority>();
		for (String role : authorities)
			listOfAuthorities.add(new GrantedAuthorityImpl(role));
		this.authorities = (Collection<GrantedAuthority>) listOfAuthorities;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public Set<Authority> getAuthoritySet() {
		return authoritySet;
	}

	public void setAuthoritySet(Set<Authority> authoritySet) {
		this.authoritySet = authoritySet;
	}

	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	@Override
	public Collection<String> validar() {
		return null;
	}
	
}